﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Customer;
using EFong.View.Proxy;

namespace EFong.View.IntergralExchange
{
    public partial class Frm_IntergralExchange : BasicForm
    {
        private Proxy.MasterServiceProxy _masterProxy { get { return Proxy.MasterServiceProxy.GetInstance(); } }
        private Proxy.BaseServiceProxy _baseProxy { get { return Proxy.BaseServiceProxy.GetInstance(); } }
        private Proxy.BusinessServiceProxy _businessProxy { get { return Proxy.BusinessServiceProxy.GetInstance(); } }

        public Frm_IntergralExchange()
        {
            InitializeComponent();
            spe_LeftIntergral.SetValue(null);
        }

        protected override void InitFields()
        {
            this.dte_SearchDate.DateTime = DateTime.Now;
        }

        protected override void Events()
        {
            this.gv_ExchangeHis.FocusedRowChanged += (s, e) =>
                {
                    CurDetail = this.gv_ExchangeHis.GetRow(e.FocusedRowHandle) as Contract.IntergralExchange;
                };

            this.btn_Search.Click += (s, e) =>
            {
                searchOrders();
            };

            this.btn_Clear.Click += (s, e) =>
                {
                    clearSearchConditions();
                };
        }

        private void clearSearchConditions()
        {
            ControlExtensions.ClearValue(
                this.dte_SearchDate
            );
        }

        private void searchOrders()
        {
            List<Contract.IntergralExchange> intergralExchangeSource = null;

            intergralExchangeSource = _businessProxy.GetIntergralExchangeByMonth(this.dte_SearchDate.GetValue(DateTime.Today)).ReturnValue;

            bool sourceIsNull = intergralExchangeSource == null;
            bool sourceIsEmpty = !sourceIsNull && intergralExchangeSource.Count == 0;

            if (sourceIsEmpty)
            {
                MsgBox.Info("没有找到符合条件的订单.");
                this.CurDetail = null;
            }

            this.bds_intergralExchange.BindingData(intergralExchangeSource);

        }

        protected override void LoadDatas()
        {
            this.bds_intergralExchange.BindingData(typeof(Contract.IntergralExchange));

            SetAuthorities();
        }

        protected void SetAuthorities()
        {
            foreach (Contract.RoleAuthorizedItem roleAuthorizedItem in AppParameters.LoginUser.Role.Authorities)
            {
                switch (roleAuthorizedItem.AuthorizedItem.Name)
                {
                    case "积分兑换":
                        bbt_Add.Enabled = roleAuthorizedItem.Allow;
                        break;
                }
            }
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void add()
        {
            Frm_IntergralExchangeEdit frm = new Frm_IntergralExchangeEdit();
            frm.Text = "兑换";
            frm.Load += (s, e) =>
            {
                frm.CurDetail = new Contract.IntergralExchange();
            };
            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
            {

            }
        }


        private void browserCustomerData()
        {
            if (CurDetail == null)
                return;

            Frm_CustomerEdit frm = new Frm_CustomerEdit();
            frm.Text = "客户详细资料";
            frm.ViewState = ViewStates.BROWSER;
            frm.Load += (s, e) =>
            {
                frm.CurCust = CurDetail.Customer;
            };
            FormOpener.ShowDialog(frm);
        }




        #region Detail
        private Contract.IntergralExchange _curDetail;
        public Contract.IntergralExchange CurDetail
        {
            get { return _curDetail; }
            set
            {
                _curDetail = value;
                onCurDetailChanged();
            }
        }

        private void onCurDetailChanged()
        {
            if (CurDetail == null)
            {

            }
            else
            {
                this.txt_Customer.SetText(CurDetail.Customer);
                this.txt_ContactPerson.SetText(CurDetail.Receiptor);
                this.txt_Item.SetText(CurDetail.Item);
                this.spe_Qty.SetValue(CurDetail.Qty);
                this.dte_ExchangeDate.SetValue(CurDetail.Date);
                this.spe_ExchangeIntergral.SetValue(CurDetail.Amount);

                if (CurDetail.Customer == null)
                    this.spe_LeftIntergral.SetValue(null);
                else
                    this.spe_LeftIntergral.SetValue(CurDetail.Customer.Intergral - CurDetail.Amount * CurDetail.Qty);
            }
        }
        #endregion

    }
}
